#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <ncs_rpg>

#define VERSION		"BETA TEST"

new ThisSkillID;

new Float:SkillAmount;

public Plugin:myinfo =
{
	name		= "[NCS RPG] Skill Bouncy Bullets",
	author		= "[RUS] DiviX",
	description	= "Skill Bouncy Bullets for NCS RPG",
	version		= VERSION,
	url			= "N/A"
};

public OnPluginStart()
{
	HookEvent("player_hurt", Event_PlayerHurt);
}

public RPG_OnSkillsRegister()
{
	ThisSkillID = RegisterSkill("bouncy", 5, 15, 15);
	SkillAmount = GetConfigValueFloat("bouncy", "amount", 100.0);
}

public Event_PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	if(IsValidPlayer(victim) && IsValidPlayer(attacker) && victim != attacker)
	{
		if(GetConfigValue("bouncy", "knife", 0) <= 0 && IsWeaponKnife(attacker))
			return;
		
		new level = GetSkillLevel(attacker, ThisSkillID);
		if(level > 0)
		{
			decl Float:pos_attacker[3], Float:ang_attacker[3], Float:pos[3];
			GetClientEyePosition(attacker, pos_attacker);
			GetClientEyeAngles(attacker, ang_attacker);
			TR_TraceRayFilter(pos_attacker, ang_attacker, MASK_ALL, RayType_Infinite, TRFilter);
			TR_GetEndPosition(pos);
			
			MakeVectorFromPoints(pos_attacker, pos, pos);
			NormalizeVector(pos, pos);
			ScaleVector(pos, SkillAmount*level);
			TeleportEntity(victim, NULL_VECTOR, NULL_VECTOR, pos);
			//SkillAmount*dmg_health;
		}
	}
}

stock bool:IsWeaponKnife(client)
{
	new entity = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
	if(entity != -1)
	{
		decl String:weapon[32];
		GetEdictClassname(entity, weapon, sizeof(weapon));
		if(StrEqual(weapon, "weapon_knife"))
			return true;
	}
	return false;
}

public bool:TRFilter(entity, contentsMask)
{
	if(entity > 0 && entity < MAXPLAYERS)
		return false;

	return true;
}